﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using PloobsEngine.Modelo;
using PloobsEngine.Utils;
#endregion

namespace PloobsEngine.Physics.XJigLib
{

    public class SphereObject : JigLibXObject
    {
        private float UniformScale = 1;

        /// <summary>
        /// Serialziation
        /// </summary>
        public SphereObject()
        {
        }

        public SphereObject(IModelo modelo, Vector3 position, Matrix orientation, float scale, float mass)
            : this(modelo,position,orientation,scale)
        {
            this.Mass = mass;            
        }

        public SphereObject(IModelo modelo, Vector3 position,Matrix orientation, float scale)
        {

            BoundingSphere bs;
            bs = modelo.GetBoudingSphere();                
            
            Body = new Body();
            CollisionSkin = new CollisionSkin(Body);
            CollisionSkin.AddPrimitive(new Sphere(Vector3.Zero, bs.Radius * scale), new MaterialProperties(0.5f, 0.7f, 0.6f)); ;
            Body.CollisionSkin = this.CollisionSkin;
            Body.MoveTo(position, orientation);
            Vector3 com = SetMass(50.0f);
            CollisionSkin.ApplyLocalTransform(new Transform(-com, Matrix.Identity));
            Body.DisableBody();
            this.Scale = new  Vector3(scale);
            this.raio = bs.Radius ;
            this.UniformScale = scale;
        }

        public SphereObject(float radius, Vector3 position, Matrix orientation, float mass)
            : this(radius, position, orientation)
        {
            this.Mass = mass;
        }

        public SphereObject( float radius, Vector3 position, Matrix orientation)
        {
            init(radius, position, orientation,1);
           
        }

        private void init(float radius, Vector3 position, Matrix orientation,float scale)
        {
            Body = new Body();
            CollisionSkin = new CollisionSkin(Body);
            CollisionSkin.AddPrimitive(new Sphere(Vector3.Zero, radius * scale), new MaterialProperties(0.5f, 0.7f, 0.6f)); ;
            Body.CollisionSkin = this.CollisionSkin;
            Body.MoveTo(position, orientation);
            Vector3 com = SetMass(10.0f);
            CollisionSkin.ApplyLocalTransform(new Transform(-com, Matrix.Identity));
            Body.EnableBody();
            this.Scale = new Vector3( scale);
            this.raio = radius;
            this.UniformScale = scale;
        }

        private float raio;


        public override void DeSerialize(XmlNode node, EngineStuff engine, IModelo model, Vector3 position, Matrix rotation)
        {
            raio = SerializerHelper.DeserializeBaseType<float>("Raio", node);
            float scale = SerializerHelper.DeserializeBaseType<float>("Scale", node);
            init(raio, position, rotation,scale);
        }
        public override void Serialize(System.Xml.XmlTextWriter writer)
        {
            SerializerHelper.SerializeBaseType<float>(raio, "Raio", writer);
            SerializerHelper.SerializeBaseType<float>(UniformScale, "Scale", writer);            

        }

        public override PhysicObjectTypes PhysicObjectTypes
        {
            get { return PhysicObjectTypes.SPHEREOBJECT; }
        }

    }
}
